<?php

namespace app\xwam\model;

/**
 * 文章类别模型
 *
 * @since 1.0.0 <2017-3-20> SoChishun <14507247@qq.com> Added.
 */
class ArticleCategoryModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $table = 't_porg_article_category';


    public function deleteCtegory($id = '') {
        if (!$id) {
            return $this->returnMsg(false, '参数无效');
        }
        // 判断是否有子项目
        $table=$this->tableName;
        $list = $this->query("select a.title from $table a inner join $table b on b.pid=a.id where b.pid in ($id)");
        $names = false;
        if ($list) {
            foreach ($list as $row) {
                $names.=',' . $row['title'];
            }
        }
        if ($names) {
            return $this->returnMsg(false, '以下项目有子项目,请先转移子项目:' . "\n" . ltrim($names, ','));
        }
        // 判断是否有引用
        $category_ids = $this->table('t_porg_article')->where(array('category_id' => array('in', $id)))->getField('category_id', true);
        if ($category_ids) {
            $names = $this->where(array('id' => array('in', $category_ids)))->getField('title', true);
            return $this->returnMsg(false, '以下项目已被占用,无法删除:' . "\n" . implode(',', $names));
        }
        $result = $this->delete($id);
        return $this->returnMsg($result);
    }
    
    function getChildIds($id) {
        return $this->base_getChildIds($id);
    }

    /**
     * 
     * @param type $selectOption
     * @param type $dataOption
     * @return type
     */
    function selectTree($selectOption = array(), $dataOption = array()) {
        $selectOption = array_merge(array('field' => 'id, title, pid, code, sort, status, create_time', 'order' => 'sort, id'), $selectOption);
        return $this->baseSelectTree($selectOption, $dataOption);
    }

    function selectTreeJson($select_options = array(), $data_options = array()) {
        $select_options = array_merge(array('field' => 'id, title as text', 'order' => 'sort, id'), $select_options);
        return $this->baseSelectTree($select_options, $data_options);
    }

    function saveCategory() {
        $rules = array(
            array('title', 'require', '名称无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
            array('site_id', 'number', 'SiteID无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        );
        if (!$this->validate($rules)->create()) {
            return $this->returnMsg(false, $this->error);
        }
        $data = xsfn_array_notempty($this->data, array('pid'));
        if (empty($this->id)) {
            unset($data['id']);
            $result = $this->add($data);
        } else {
            if (isset($data['pid']) && $this->id == $this->pid) {
                return $this->returnMsg(false, '父级不能是自己!'); // 2016-8-5 修正
            }
            $result = $this->save($data);
        }
        return $this->returnMsg($result);
    }

    /**
     * 复制部门
     * @return type
     * @since 1.0 <2016-1-14> SoChishun Added.
     */
    function copyCategory($id) {
        $table = $this->tableName;
        M()->execute("insert into $table (title, pid, `code`, status, site_id) select concat(title,'_new'), pid, `code`, status, site_id from $table where id=$id");
        return $this->returnMsg(true);
    }

    function changeStatus($id, $status) {
        $result = $this->where(array('id' => $id))->setField(array('status' => $status));
        return $this->returnMsg($result);
    }

    function changeSort($id, $sort) {
        $result = $this->where(array('id' => $id))->setField(array('sort' => $sort));
        return $this->returnMsg($result);
    }

    /**
     * 批量更新外键对应的标题
     * @param type $all
     * @since 1.0 2017-3-17 SoChishun Added.
     */
    static function updateForeignTitle($all = false, $id = '') {
        M()->execute("update t_porg_article a left join t_porg_article_category c on a.category_id=c.id set a.category_title=c.title where " . ($id ? "a.category_id in ($id)" : "a.category_id > 0") . ($all || $id ? '' : " and a.category_title=''"));
    }

}
